package com.alpsoft.spotcheck.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

/**
 * 安卓sqlite数据的工具类。
 * 实现数据库表的创建功能。
 * @author Nasir
 *
 */
public class DataBaseHelper extends SDCardSQLiteOpenHelper {

	public DataBaseHelper(Context context, String name, CursorFactory factory, int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		//点检记录表
		StringBuilder sb = new StringBuilder();
		sb.append("CREATE TABLE [EQ_CHECK_RECORD] (");
		sb.append("[CHECK_RECORD_ID] NVARCHAR NOT NULL PRIMARY KEY,");
		sb.append("[CHECK_ITEM_STRATEGY_ID] NVARCHAR,");
		sb.append("[CHECK_USER_ID] NVARCHAR,");
		sb.append("[CHECK_TIME] NVARCHAR,");
		sb.append("[CHECK_RESULT] NVARCHAR,");
		sb.append("[IS_EXCEPTION] NVARCHAR,");
		sb.append("[PLAN_DATE] NVARCHAR,");
		sb.append("[END_TIME] NVARCHAR,");
		sb.append("[START_TIME] NVARCHAR,");
		sb.append("[IS_DOWNLOAD] NVARCHAR);");
		db.execSQL(sb.toString());
		//点检项目表
		sb = new StringBuilder();
		sb.append("CREATE TABLE [EQ_CHECK_ITEM] (");
		sb.append("[CHECK_ITEM_ID] NVARCHAR NOT NULL PRIMARY KEY,");
		sb.append("[EQUIPMENT_ID] NVARCHAR,");
		sb.append("[ITEM_CODE] NVARCHAR,");
		sb.append("[ITEM_CONTENT] NVARCHAR,");
		sb.append("[ITEM_CYCLE] NVARCHAR,");
		sb.append("[ITEM_TYPE] NVARCHAR,");
		sb.append("[UNIT] NVARCHAR,");
		sb.append("[DOWN_VALUE] NVARCHAR,");
		sb.append("[UP_VALUE] NVARCHAR,");
		sb.append("[HANDLE_WAY] NVARCHAR,");
		sb.append("[REMARKS] NVARCHAR,");
		sb.append("[LAST_DATE] NVARCHAR,");
		sb.append("[CYCLE_UNIT] NVARCHAR,");
		sb.append("[CYCLE_TYPE] NVARCHAR,");
		sb.append("[STANDARD] NVARCHAR,");
		sb.append("[ITEM_FN] NVARCHAR);");
		db.execSQL(sb.toString());
		//设备表
		sb = new StringBuilder();//13col?
		sb.append("CREATE TABLE [EQ_EQUIPMENT] (");
		sb.append("[EQUIPMENT_ID] NVARCHAR NOT NULL PRIMARY KEY,");
		sb.append("[DEPT_ID] NVARCHAR,");
		sb.append("[SELF_CODE] NVARCHAR,");
		sb.append("[PARENT_CODE] NVARCHAR,");
		sb.append("[SCAN_CODE] NVARCHAR,");
		sb.append("[EQUIPMENT_NAME] NVARCHAR,");
		sb.append("[SUBMIT_USER_ID] NVARCHAR,");
		sb.append("[EQUIPMENT_TYPE] NVARCHAR,");
		sb.append("[EQUIPMENT_STATUS] NVARCHAR,");
		sb.append("[REMARK] NVARCHAR);");
		db.execSQL(sb.toString());
		//附件表
		sb = new StringBuilder();
		sb.append("CREATE TABLE [EQ_ATTACHMENT] (");
		sb.append("[ATTACHMENT_ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,");
		sb.append("[ATTACHMENT_NAME] NVARCHAR,");
		sb.append("[FILE_PATH] NVARCHAR,");
		sb.append("[TABLE_NAME] NVARCHAR,");
		sb.append("[KEY] NVARCHAR,");
		sb.append("[VALUE] NVARCHAR);");
		db.execSQL(sb.toString());
		//异常表
		sb = new StringBuilder();
		sb.append("CREATE TABLE [RP_EXCEPTION_SUBMIT] (");
		sb.append("[EXCEPTION_SUBMIT_ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,");
		sb.append("[SUBMIT_USER_ID] NVARCHAR,");
		sb.append("[EQUIPMENT_ID] NVARCHAR,");
		sb.append("[RESOURCE_ID] NVARCHAR,");
		sb.append("[SUBMIT_TIME] NVARCHAR,");
		sb.append("[SUBMIT_CONTENT] NVARCHAR,");
		sb.append("[STATUS] NVARCHAR,");
		sb.append("[DX_RESULT] NVARCHAR,");
		sb.append("[RESOURCE_TYPE] NVARCHAR,");
		sb.append("[DX_TIME] NVARCHAR,");
		sb.append("[REMARKS] NVARCHAR,");
		sb.append("[DX_USER_ID] NVARCHAR,");
		sb.append("[FLAG] NVARCHAR);");
		db.execSQL(sb.toString());
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS [EQ_CHECK_RECORD]");
		db.execSQL("DROP TABLE IF EXISTS [EQ_EQUIPMENT]");
		db.execSQL("DROP TABLE IF EXISTS [EQ_CHECK_ITEM]");
		db.execSQL("DROP TABLE IF EXISTS [EQ_ATTACHMENT]");
		db.execSQL("DROP TABLE IF EXISTS [RP_EXCEPTION_SUBMIT]");
		db.execSQL("DROP TABLE IF EXISTS [EQ_RH_RECORD]");
		db.execSQL("DROP TABLE IF EXISTS [EQ_RH_ITEM]");
		db.execSQL("DROP TABLE IF EXISTS [EQ_RH_EQUIPMENT]");
		db.execSQL("DROP TABLE IF EXISTS [EQ_RH_ATTACHMENT]");
		onCreate(db);
	}

	@Override
	public void onOpen(SQLiteDatabase db) {
		super.onOpen(db);
	}
}
